//
//  XFlowerListViewController.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/18.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import UIKit
import SVProgressHUD
class XFlowerListViewController: XBaseController {
    lazy var flowerListViewModel:XFlowerListViewModel = XFlowerListViewModel()
     var collectionView:UICollectionView?
//    var isPullUp:Bool = false
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
//        view.backgroundColor = UIColor.white
        

    }
    func back(){
        dismiss(animated: true, completion: nil)
    }
    override func loadData() {
        SVProgressHUD.showInfo(withStatus: "正在加载中")
        flowerListViewModel.getFlowerList(isPull: false) { (isSuccess) in
            self.collectionView?.reloadData()
            SVProgressHUD.dismiss()
        }
    }
    override func loadMore(){
        SVProgressHUD.showInfo(withStatus: "正在加载中")
        flowerListViewModel.getFlowerList(isPull: true) { (isSuccess) in
            self.collectionView?.reloadData()
            self.isPullUp = false
            SVProgressHUD.dismiss()
        }
    }

   

}
extension XFlowerListViewController{
    ///设置UI
    fileprivate func setupUI(){
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 64, width: UIScreen.cz_screenWidth(), height: UIScreen.cz_screenHeight()-64), collectionViewLayout: UICollectionViewFlowLayout())//UICollectionView(frame: )
        collectionView?.delegate = self
        collectionView?.dataSource = self
        //        let layout = UICollectionViewLayout()
        //此处必须要有创见一个UICollectionViewFlowLayout的对象
        
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        let itemW = (UIScreen.cz_screenWidth()-15)/2
        let itemH = (UIScreen.cz_screenWidth()-15)/2
        layout.itemSize = CGSize(width: itemW, height: itemH)
        
        //同一行相邻两个cell的最小间距
        
        layout.minimumInteritemSpacing = 5;
        
        //最小两行之间的间距
        
        layout.minimumLineSpacing = 5;
        collectionView?.collectionViewLayout = layout
        collectionView?.backgroundColor = UIColor.lightGray
        //        collectionView.numberOfSections = 1
        collectionView?.register(XFlowerCell.self, forCellWithReuseIdentifier: "cellId")
        
        self.navigationController?.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", target: self, action: #selector(back))
        view.addSubview(collectionView!)
    }
}
// MARK: - collection 代理及数据源方法
extension XFlowerListViewController:UICollectionViewDelegate,UICollectionViewDataSource{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return flowerListViewModel.modelList.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as? XFlowerCell
        cell?.model = flowerListViewModel.modelList[indexPath.row]
        cell?.backgroundColor = UIColor.cz_random()
        return cell ?? XFlowerCell()
        
    }
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        
        //行
        let row = indexPath.row
        //        print(" will show row == \(row)")
        //分组
        let se = indexPath.section
        //分组数
        let section = collectionView.numberOfSections - 1
        if row < 0 || se < section{
            //            if row < 0 || section<0{
            return
        }
        //如果是最后一个分组了
        let count = collectionView.numberOfItems(inSection: section)//collectionView.numberOfRows(inSection: section)
        ///
        let refrechRow = count - 2
        if(row == refrechRow && !isPullUp){
            print("到\(refrechRow)了，准备上拉刷新了")
            
            isPullUp = true
            loadMore()
        }
        
    }
    
    
    

}
